﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using PlanProd.Contracts.enums;
using Web.Framework.Svc;

namespace PlanProd.Contracts
{
    [Serializable]
    [DataContract]
    public class Desvio : IAuditable, ILogicalRemoved
    {
        public Desvio()
        {
            DesvioDetalles = new List<DesvioDetalle>();
        }

        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public int PlanProduccionId { get; set; }
        
        public TipoDesvio TipoDesvio
        {
            get
            {
                return (TipoDesvio)IntTipoDesvio;
            }
            set
            {
                IntTipoDesvio = (char)value;
            }
        }

        [DataMember]
        public int IntTipoDesvio { get; set; }

        [DataMember]
        public int CausaDesvioId { get; set; }

        [DataMember]
        public string CausaDesvioNombre { get; set; }

        [DataMember]
        public List<DesvioDetalle> DesvioDetalles { get; set; }        

        [DataMember]
        public string Estado { get; set; }

        [DataMember]
        public string UsuarioCreador { get; set; }

        [DataMember]
        public string UsuarioAprobador { get; set; }

        [DataMember]
        public DateTime FechaCreacion { get; set; }

        [DataMember]
        public DateTime? FechaAprobacion { get; set; }

        [DataMember]
        public int ProductoId { get; set; }

        [DataMember]
        public int PlanProduccionDetallePrimeraId { get; set; }

        [DataMember]
        public int? PlanProduccionDetalleSegundaId { get; set; }

        [DataMember]
        public string ProductoSapArticuloNombre { get; set; }

        [DataMember]
        public string ProductoIdExterno { get; set; }

        [DataMember]
        public DateTime? FechaDeDesvio { get; set; }

        [DataMember]
        public string ProductoSapColorNombre { get; set; }

        public bool EsMovimientoValido()
        {
            int sumaOriginal = 0;
            int sumaFinal = 0;
            foreach (DesvioDetalle desvioDetalle in this.DesvioDetalles)
            {
                sumaOriginal += desvioDetalle.CantidadInicial;
                sumaFinal += desvioDetalle.CantidadFinal;
            }

            return sumaOriginal == sumaFinal;
        }

        [DataMember]
        public DateTime AuditFechaCreacion { get; set; }

        [DataMember]
        public string AuditUserNameCreacion { get; set; }

        [DataMember]
        public DateTime? AuditFechaModificacion { get; set; }

        [DataMember]
        public string AuditUserNameModificacion { get; set; }

        [DataMember]
        public bool Removed { get; set; }
    }   
}
